package com.cxq.finalproject.adapter;

import static android.content.Context.MODE_PRIVATE;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.cxq.finalproject.R;
import com.cxq.finalproject.entity.ShoppingCarItem;
import com.cxq.finalproject.util.MyClothHelper;
import com.cxq.finalproject.util.MyUserHelper;

import java.util.List;

public class ShoppingCarAdapter extends RecyclerView.Adapter<ShoppingCarViewHolder>{
    private LayoutInflater mInflate;
    private Context mContext;
    private List<ShoppingCarItem> mData;

    public ShoppingCarAdapter(Context context, LayoutInflater mInflate, List<ShoppingCarItem> mData){
        this.mContext=context;
        this.mInflate = mInflate;
        this.mData = mData;
    }

    @NonNull
    @Override
    public ShoppingCarViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mInflate.inflate(R.layout.shopping_car_item,viewGroup,false);
        ShoppingCarViewHolder shoppingCarViewHolder = new ShoppingCarViewHolder(view);
        return shoppingCarViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ShoppingCarViewHolder shoppingCarViewHolder, @SuppressLint("RecyclerView") int position) {
        shoppingCarViewHolder.tvprice.setText("￥" + mData.get(position).getPrice());
        shoppingCarViewHolder.mImageview.setImageResource(mData.get(position).getImg());
        shoppingCarViewHolder.tvIntroduce.setText(mData.get(position).getIntroduce());
        shoppingCarViewHolder.itemView.setBackgroundColor(Color.WHITE);
        shoppingCarViewHolder.itemView.setBackgroundResource(R.drawable.button_circle_shape);
        shoppingCarViewHolder.imgSrc = mData.get(position).getSrcName();
        shoppingCarViewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //归还商品
                SQLiteDatabase db = shoppingCarViewHolder.myClothHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put(MyClothHelper.Isload,"0");
                values.put(MyClothHelper.LoadUserId,"-1");
                values.put(MyClothHelper.Name, shoppingCarViewHolder.imgSrc);
                long rid = db.update(MyClothHelper.TABLE_NAME,values,"name=?",new String[]{shoppingCarViewHolder.imgSrc});
                db.close();
                removeItem(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    //添加数据
    public void addItem(ShoppingCarItem data, int position) {
        mData.add(position, data);
        notifyItemInserted(position);
    }
    //删除数据
    public void removeItem(int position) {
//        int position = mData.indexOf(data);
        mData.remove(position);
        notifyItemRemoved(position);
    }
}

class ShoppingCarViewHolder extends RecyclerView.ViewHolder{
    TextView tvIntroduce;
    TextView tvprice;
    ImageView mImageview;
    String imgSrc;
    Button button;
    MyClothHelper myClothHelper;
    private SharedPreferences sp;
    public ShoppingCarViewHolder(@NonNull View itemView) {
        super(itemView);
        tvIntroduce = (TextView) itemView.findViewById(R.id.textView9);
        tvprice = (TextView) itemView.findViewById(R.id.textView5);
        mImageview = (ImageView) itemView.findViewById(R.id.imageView5);
        button = (Button) itemView.findViewById(R.id.button12);
        myClothHelper = new MyClothHelper(itemView.getContext(), MyClothHelper.DB_NAME,null,1);
        sp = itemView.getContext().getSharedPreferences("Login_UserInfo", MODE_PRIVATE);
    }

}
